package com.zhoug.zmui;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhoug.common.interfaces.OnExpandChangeListener;
import com.zhoug.common.utils.AttributeUtils;
import com.zhoug.common.utils.ResourceUtils;

import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

/**
 * 收缩展开控件
 *
 * @Author: zhoug
 * @Date: 2024-07-16
 * @Description:
 */
public class ZMUIExpandView extends FrameLayout {
    private static final String TAG = ">>>ZMUIExpandView";
    private View mRoot;
    private TextView mTextView;
    private ImageView mImageView;
    /**
     * 标记展开状态
     * true:展开,false:收缩
     */
    protected boolean mExpanded = false;
    protected @DrawableRes int mIconRes;
    protected @DrawableRes int mExpandIconRes;
    protected ColorStateList mIconTint;
    protected String mText;
    protected String mExpandText;
    protected int mTextColor;
    protected int mExpandTextColor;
    /**
     * 是否自动切换展开/关闭,
     * 设置为true,那么点击会切换expanded状态,
     * 设置为false,点击只会设置expanded状态为true,false状态需要显示调用{@link #setExpanded(boolean)}方法,这样可以防止重复点击
     */
    protected boolean mAutoExpand;

    private OnExpandChangeListener<ZMUIExpandView> mOnExpandChangeListener;

    public ZMUIExpandView(@NonNull Context context) {
        this(context, null);
    }

    public ZMUIExpandView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.zmui_style_ZMUIExpandView);
    }

    public ZMUIExpandView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    private void init(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZMUIExpandView, defStyleAttr, R.style.zmui_style_ZMUIExpandView);
        mIconRes = a.getResourceId(R.styleable.ZMUIExpandView_zmui_iconDrawable, R.drawable.zmui_icon_expand_normal);
        mExpandIconRes = a.getResourceId(R.styleable.ZMUIExpandView_zmui_expandIconDrawable, R.drawable.zmui_icon_expand_expand);
        mTextColor = a.getColor(R.styleable.ZMUIExpandView_zmui_textColor, ContextCompat.getColor(context, R.color.zmui_textColor));
        mExpandTextColor = a.getColor(R.styleable.ZMUIExpandView_zmui_expandTextColor, 0);
        float textSize = a.getDimension(R.styleable.ZMUIExpandView_zmui_textSize, context.getResources().getDimension(R.dimen.zmui_textSize));
        int textStyle = a.getInt(R.styleable.ZMUIExpandView_zmui_textStyle, Typeface.NORMAL);
        mText = a.getString(R.styleable.ZMUIExpandView_zmui_text);
        mExpandText = a.getString(R.styleable.ZMUIExpandView_zmui_expandText);
        int iconSize = a.getDimensionPixelSize(R.styleable.ZMUIExpandView_zmui_iconSize, ResourceUtils.dpTopx(12));
        mIconTint = a.getColorStateList(R.styleable.ZMUIExpandView_zmui_iconTint);
        int drawablePadding = a.getDimensionPixelOffset(R.styleable.ZMUIExpandView_zmui_drawablePadding, ResourceUtils.dpTopx(5));
        mExpanded = a.getBoolean(R.styleable.ZMUIExpandView_zmui_expand, false);
        mAutoExpand = a.getBoolean(R.styleable.ZMUIExpandView_zmui_autoExpand, true);
        boolean singleLine = a.getBoolean(R.styleable.ZMUIExpandView_android_singleLine, true);
        int gravity = a.getInt(R.styleable.ZMUIExpandView_android_gravity, Gravity.CENTER);

        a.recycle();

        mRoot = LayoutInflater.from(context).inflate(R.layout.zmui_expand_view, this, false);
        mTextView = mRoot.findViewById(R.id.expandViewName);
        mImageView = mRoot.findViewById(R.id.expandViewIcon);

        mTextView.setEllipsize(TextUtils.TruncateAt.END);
        setSingleLine(singleLine);
        setTextSize(textSize);
        setTypeface(textStyle);
        setIconSize(iconSize);
        setDrawablePadding(drawablePadding);
        setGravity(gravity);

        _setTextColor();
        _setIcon();
        setDefName();

        setClickable(true);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAutoExpand) {
                    setExpanded(!mExpanded, true);
                } else {
                    //未展开
                    if (!mExpanded) {
                        setExpanded(true, true);
                    }
                }
            }
        });
        addView(mRoot);
    }


    public void setIconRes(int iconRes) {
        mIconRes = iconRes;
        if (!mExpanded) {
            _setIcon();
        }
    }

    public void setExpandIconRes(int expandIconRes) {
        mExpandIconRes = expandIconRes;
        if (mExpanded) {
            _setIcon();
        }
    }

    public void setTextColor(int textColor) {
        this.mTextColor = textColor;
        if (!mExpanded) {
            _setTextColor();
        }
    }

    public void setExpandTextColor(int expandTextColor) {
        this.mExpandTextColor = expandTextColor;
        if (mExpanded) {
            _setTextColor();
        }
    }

    public void setText(String text) {
        this.mText = text;
        if (!mExpanded) {
            setDefName();
        }
    }

    public void setExpandText(String expandText) {
        this.mExpandText = expandText;
        if (mExpanded) {
            setDefName();
        }
    }

    public void setIconSize(int iconSize) {
        ViewGroup.LayoutParams layoutParams = mImageView.getLayoutParams();
        layoutParams.width = iconSize;
        layoutParams.height = iconSize;
        mImageView.setLayoutParams(layoutParams);
    }

    public void setIconTint(ColorStateList iconTint) {
        this.mIconTint = iconTint;
        AttributeUtils.setImageTintList(mImageView, mIconTint);

    }

    public void setDrawablePadding(int drawablePadding) {
        ViewGroup.LayoutParams layoutParams = mTextView.getLayoutParams();
        if (layoutParams instanceof MarginLayoutParams) {
            ((MarginLayoutParams) layoutParams).rightMargin = drawablePadding;
            mTextView.setLayoutParams(layoutParams);
        }
    }

    public void setGravity(int gravity){
        ViewGroup.LayoutParams layoutParams = mRoot.getLayoutParams();
        if (layoutParams instanceof LayoutParams flayoutParams) {
            flayoutParams.gravity = gravity;
            mRoot.setLayoutParams(layoutParams);
        }
    }

    /**
     * 是否自动切换展开/关闭,
     *
     * @param autoExpand 设置为true,那么点击会切换expanded状态,
     *                   设置为false,点击只会设置expanded状态为true,false状态需要显示调用{@link #setExpanded(boolean)}方法,这样可以防止重复点击
     */
    public void setAutoExpand(boolean autoExpand) {
        this.mAutoExpand = autoExpand;
    }

    public void setSingleLine(boolean singleLine) {
        mTextView.setSingleLine(singleLine);
    }

    /**
     * 设置文字大小
     *
     * @param textSize px
     */
    public void setTextSize(float textSize) {
        AttributeUtils.setTextSize(mTextView, textSize, 0);
    }

    public void setTypeface(int typeface) {
        AttributeUtils.setTypeface(mTextView, typeface);
    }


    public void setDefName() {
        if (mExpanded) {
            setName(mExpandText != null ? mExpandText : mText);
        } else {
            setName(mText);
        }
    }

    public void setName(String name) {
        mTextView.setText(name);
    }

    protected void _setTextColor() {
        if (mExpanded) {
            mTextView.setTextColor(mExpandTextColor != 0 ? mExpandTextColor : mTextColor);
        } else {
            mTextView.setTextColor(mTextColor);
        }
    }

    /**
     * 设置图标
     */
    protected void _setIcon() {
        if (mExpanded) {
            mImageView.setImageResource(mExpandIconRes);
        } else {
            mImageView.setImageResource(mIconRes);
        }
        AttributeUtils.setImageTintList(mImageView, mIconTint);
    }

    /**
     * 展开状态
     *
     * @param expanded true:展开,false:收缩
     */
    public void setExpanded(boolean expanded) {
        setExpanded(expanded, false);
    }

    /**
     * 标记展开状态
     * true:展开,false:收缩
     */
    public void setExpanded(boolean expanded, boolean invokeCallback) {
        if (this.mExpanded != expanded) {
            this.mExpanded = expanded;
            _setTextColor();
            _setIcon();
            if (invokeCallback && mOnExpandChangeListener != null) {
                mOnExpandChangeListener.onExpand(this, this.mExpanded);
            }
        }
    }

    /**
     * 设置展开/收缩状态改变监听
     *
     * @param onExpandChangeListener
     */
    public void setOnExpandChangeListener(OnExpandChangeListener<ZMUIExpandView> onExpandChangeListener) {
        this.mOnExpandChangeListener = onExpandChangeListener;
    }

    private ZMUIExpandView self() {
        return this;
    }
}
